package cn.exrick.xboot.base.login

import cn.exrick.xboot.base.service.IAreaService
import cn.exrick.xboot.base.service.IDepartmentUserService
import cn.exrick.xboot.core.common.constant.CommonConstant
import cn.exrick.xboot.core.common.utils.ResultUtil
import cn.exrick.xboot.core.common.utils.SecurityUtil
import cn.exrick.xboot.core.common.vo.Result
import cn.exrick.xboot.core.dao.RoleDao
import cn.exrick.xboot.core.entity.Role
import cn.exrick.xboot.core.service.RoleService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping("/xboot/login")
class LoginController {


    @Autowired
    private lateinit var loginService: LoginService

    @Autowired
    private lateinit var iDepartmentUserService: IDepartmentUserService

    @Autowired
    private lateinit var securityUtil: SecurityUtil

    @Autowired
    private lateinit var roleDao: RoleDao

    @Autowired
    private lateinit var iAreaService: IAreaService

    @Autowired
    private lateinit var roleService: RoleService

    @PostMapping("/removeUser")
    fun removeUser(ids: String) : Result<Any>? = loginService.removeUser(ids)


    @PostMapping(value = ["/getListByType"])
    fun roleGetByType(): Result<List<Role>>? {

        return iDepartmentUserService.getDepartmentUserByUserId(securityUtil.currUser.id)?.run {

            when (type) {
                0 -> {
                    ResultUtil.data(listOf(when (userLevel) {
                        1 -> {
                            roleDao.findRoleByName(CommonConstant.ROLE_CITY_DEPARTMENT_LEADER)
                        }
                        2 -> {
                            roleDao.findRoleByName(CommonConstant.ROLE_COUNTRY_DEPARTMENT_LEADER)
                        }
                        else -> {
                            roleDao.findRoleByName(CommonConstant.ROLE_TOWN_DEPARTMENT_LEADER)
                        }
                    }))
                }
                else -> {
                   ResultUtil.data(roleDao.findRoleByType(iAreaService.getById(securityUtil.currUser.areaId)?.areaLevel))
                }
            }
        }
    }

}